import { Controller, Get } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { HealthCheckService } from '../services/health-check.service';

@Controller('health')
@ApiTags('健康检查')
export class HealthController {
  constructor(private readonly healthCheckService: HealthCheckService) {}

  @Get()
  @ApiOperation({ summary: '检查所有服务健康状态' })
  @ApiResponse({ 
    status: 200, 
    description: '健康检查结果',
    schema: {
      example: {
        database: {
          status: 'connected',
          config: {
            type: 'mysql',
            host: 'localhost',
            port: 3306,
            database: 'kv_blog'
          }
        },
        redis: {
          status: 'connected',
          config: {
            host: 'localhost',
            port: 6379,
            db: 0
          }
        },
        overall: 'healthy'
      }
    }
  })
  async checkHealth() {
    return await this.healthCheckService.getDetailedHealthStatus();
  }

  @Get('simple')
  @ApiOperation({ summary: '简单健康检查' })
  @ApiResponse({ 
    status: 200, 
    description: '简单检查结果',
    schema: {
      example: {
        status: 'healthy',
        timestamp: '2024-01-15T10:30:00.000Z'
      }
    }
  })
  async simpleHealthCheck() {
    const isHealthy = await this.healthCheckService.checkAllConnections();
    return {
      status: isHealthy ? 'healthy' : 'unhealthy',
      timestamp: new Date().toISOString(),
    };
  }

  @Get('database')
  @ApiOperation({ summary: '检查数据库连接' })
  async checkDatabase() {
    const isConnected = await this.healthCheckService.checkDatabaseConnection();
    return {
      status: isConnected ? 'connected' : 'disconnected',
      service: 'database',
      timestamp: new Date().toISOString(),
    };
  }

  @Get('redis')
  @ApiOperation({ summary: '检查Redis连接' })
  async checkRedis() {
    const isConnected = await this.healthCheckService.checkRedisConnection();
    return {
      status: isConnected ? 'connected' : 'disconnected',
      service: 'redis',
      timestamp: new Date().toISOString(),
    };
  }
}